package com.situ.springboot.controller;

import com.situ.springboot.pojo.Banji;
import com.situ.springboot.pojo.Student;
import com.situ.springboot.pojo.StudentSearchVO;
import com.situ.springboot.service.IBanjiService;
import com.situ.springboot.service.IStudentService;
import com.situ.springboot.util.JSONResult;
import com.situ.springboot.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

// SpringMVC
@Controller
@RequestMapping("/student")
public class StudentController {
    // IStudentService studentService = new StudentServiceImpl();
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IBanjiService banjiService;

    // http://localhost:8080/student/selectAll
    @RequestMapping("/selectAll")
    @ResponseBody    // 以json形式返回数据
    public List<Student> selectAll() {
        // Ctrl+Shift+/
        /*List<Student> list = new ArrayList<>();
        Student student1 = new Student(1, "zhangsan1", 23, "nan");
        Student student2 = new Student(2, "zhangsan2", 23, "nan");
        Student student3 = new Student(3, "zhangsan3", 23, "nan");
        list.add(student1);
        list.add(student2);
        list.add(student3);*/
        List<Student> list  = studentService.selectAll();
        return list;
    }

    // http://localhost:8080/student/getStudentListPage
    @RequestMapping("/getStudentListPage")
    public String getStudentListPage(Model model) { // 返回值String代表要转发的界面
        // List<Student> list  = studentService.selectAll();
        // model.addAttribute("list", list);
        return "/WEB-INF/student_list.jsp";

    }

    // /student/deleteById?id=1
    @RequestMapping("/deleteById")
    @ResponseBody
    public JSONResult deleteById(Integer id) {
        // sout
        System.out.println("id: " + id);
        // Alt+Enter
        studentService.deleteById(id);

        // 删除完了之后应该，再去完成一次查找所有展示数据的过程
        // return "redirect:/student/getStudentListPage";

        return JSONResult.ok("删除成功");
    }

    // http://localhost:8080/student/selectByPage?page=1&limit=10&id=9&name=zs&beginTime=2022&endTime=2023
    // page代表：第几页数据   limit:每页有多少条数据
    @RequestMapping("/selectByPage")
    @ResponseBody
    public LayUITableResult selectByPage(Integer page, Integer limit, StudentSearchVO studentSearchVO) {
        LayUITableResult layUITableResult = studentService.selectByPage(page, limit, studentSearchVO);
        return layUITableResult;
    }

    @RequestMapping("/getStudentAddPage")
    public String getStudentAddPage(Model model) {
        List<Banji> banjiList = banjiService.selectAll();
        model.addAttribute("banjiList", banjiList);
        return "/WEB-INF/student_add.jsp";
    }

    // {'name':'zhansgan','age':23,'gender':'男'}
    @RequestMapping("/add")
    @ResponseBody
    public JSONResult add(Student student) {
        studentService.add(student);
        return JSONResult.ok("添加成功");
    }

    @RequestMapping("/getStudentUpdatePage")
    public String getStudentUpdatePage(Integer id, Model model) {
        /*根据id查出要更新的学生信息*/
        Student student = studentService.selectById(id);
        // 把查出来的数据放到Model内存里面，然后转发到student_update.jsp页面进行展示
        model.addAttribute("student", student);
        return "/WEB-INF/student_update.jsp";
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(Student student) {
        studentService.update(student);
        return JSONResult.ok("更新成功");
    }

    // '10,16'
    @RequestMapping("/deleteAll")
    @ResponseBody
    public JSONResult deleteAll(Integer[] ids) {
        studentService.deleteAll(ids);
        return JSONResult.ok("删除成功");
    }
}
